package com.zbkj.dal.pojo.response.ba;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zbkj.dal.base.enums.*;
import com.zbkj.dal.entity.ba.BizImg;
import com.zbkj.dal.entity.ba.Share;
import com.zbkj.dal.pojo.vo.LogisticsResultVo;
import com.zbkj.dal.pojo.vo.ReservationStoreVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
public class ShareDetailResponse {
    // 用户信息
    @ApiModelProperty("用户信息")
    private UserInfo userInfo;

    // ------------------ 商品信息 ------------------

    @ApiModelProperty("商品图片")
    private String productImg;

    @ApiModelProperty("商品名称")
    private String productName;

    @ApiModelProperty("供享订单编号")
    private String orderNo;

    /**
     * @see com.zbkj.dal.base.enums.BizSharedOrderStatus
     */
    @ApiModelProperty("订单状态")
    private String status;

    @ApiModelProperty("订单状态")
    private String statusText;

    /**
     * 提示内容
     */
    @ApiModelProperty("提示内容")
    private String tips;

    /**
     * @see com.zbkj.dal.base.enums.BizSharedOrderStatus.SubStatus
     */
    @ApiModelProperty("订单子状态")
    private String subStatus;

    @ApiModelProperty("订单子状态")
    private String subStatusText;

    /**
     * @see com.zbkj.dal.base.enums.BizShareModeEnum
     */
    @ApiModelProperty("供享模式")
    private String shareMode;

    @ApiModelProperty("供享模式名称")
    private String shareModeName;

    @ApiModelProperty("初步估价区间-起始")
    private String estimatePriceStart;
    @ApiModelProperty("初步估价区间-结束")
    private String estimatePriceEnd;
    @ApiModelProperty("初评瑕疵&污渍描述")
    private String remark;

    @ApiModelProperty("用户提交的信息")
    private Detail userSubmit;
    @ApiModelProperty("用户提交备注")
    private String description;

    @ApiModelProperty("初评结果的信息")
    private Detail initialEvaluation;

    @ApiModelProperty("图片信息")
    private Map<RecyclePositionEnums, BizImg> pics;

    @ApiModelProperty("创建时间")
    private LocalDateTime createdTime;

    @ApiModelProperty("完成时间")
    private LocalDateTime completedTime;

    // 其他补充信息可能不同阶段会出现不同的信息

    @ApiModelProperty("预约取件信息")
    private PickUpAppt pickUpAppt;

    @ApiModelProperty("预约上门信息")
    private InStoreAppt InStoreAppt;

    @ApiModelProperty("订单物流信息")
    private List<LogisticsResultVo> orderLogisticsResponse;

    @ApiModelProperty("订单预约信息")
    private ReservationStoreVO reservationStoreVO;

    @ApiModelProperty("最终估价信息")
    private FinalEstimation finalEstimation;

    @Data
    public static class UserInfo {
        @ApiModelProperty("用户ID")
        private Integer userId;

        @ApiModelProperty("用户名称")
        private String userName;

        @ApiModelProperty("用户手机号")
        private String phone;

        @ApiModelProperty("用户等级")
        private String userLevel;
    }


    @Data
    public static class Detail {

        @ApiModelProperty("品类")
        private String productType;

        @ApiModelProperty("品牌")
        private String brand;

        @ApiModelProperty("型号/编码")
        private String code;

        @ApiModelProperty("成色")
        private String quality;

        @ApiModelProperty("配件")
        private String parts;

        @ApiModelProperty("(初评之后才有)初步估价区间-起始")
        private String estimatePriceStart;

        @ApiModelProperty("(初评之后才有)初步估价区间-结束")
        private String estimatePriceEnd;

    }

    @Data
    @ApiModel("预约取件信息")
    @EqualsAndHashCode(callSuper = true)
    public static class PickUpAppt extends ReturnBase {

        @ApiModelProperty("订单地址ID")
        private Integer id;

        @ApiModelProperty(value = "快递名称", example = "顺丰速递")
        private String deliveryName;

        @ApiModelProperty(value = "取件码", example = "2039")
        private String pickupCode;

        @ApiModelProperty(value = "寄件人名称", example = "张三")
        private String senderName;

        @ApiModelProperty(value = "寄件人电话", example = "12821092321")
        private String senderPhone;

        @ApiModelProperty(value = "寄件人地址", example = "上海市**区")
        private String senderAddress;
    }

    @Data
    @ApiModel("上门取件")
    @EqualsAndHashCode(callSuper = true)
    public static class InStoreAppt extends ReturnBase {

        @ApiModelProperty("预约编号")
        private String reservationNo;

        @ApiModelProperty(value = "商户名称")
        private String name;

        @ApiModelProperty(value = "商户手机号")
        private String phone;

        @ApiModelProperty(value = "商户详细地址")
        private String address;

        @ApiModelProperty(value = "纬度")
        private String latitude;

        @ApiModelProperty(value = "经度")
        private String longitude;
    }

    @Data
    @ApiModel("预约时间展示基础数据")
    public static class ReturnBase {

        @ApiModelProperty(value = "日期描述", example = "今天")
        private String dateDesc;

        @ApiModelProperty(value = "取件时间段", example = "09:00:00")
        @JsonFormat(pattern = "HH:mm")
        @DateTimeFormat(pattern = "HH:mm")
        private LocalTime startTime;

        @ApiModelProperty(value = "取件时间段", example = "09:00:00")
        @JsonFormat(pattern = "HH:mm")
        @DateTimeFormat(pattern = "HH:mm")
        private LocalTime endTime;
    }

    @Data
    public static class FinalEstimation {

        @ApiModelProperty("最终估价")
        private String estimation;

        @ApiModelProperty("雅识")
        private String balance;

//        @ApiModelProperty("终评瑕疵&污渍描述")
//        private String finalDescription;
    }

    public static ShareDetailResponse buildByShare(Share share, Map<RecyclePositionEnums, BizImg> bizImgs, String tips) {
        ShareDetailResponse shareDetailResponse = new ShareDetailResponse();
        // todo 用户信息补充
        BizImg bizImg = bizImgs.get(RecyclePositionEnums.POSITIVE);
        shareDetailResponse.setProductImg(Objects.isNull(bizImg) ? "" : bizImg.getPath());
        shareDetailResponse.setProductName(share.getBrand() + share.getProductType());
        // 订单信息
        shareDetailResponse.setOrderNo(share.getOrderNo());
        shareDetailResponse.setShareMode(share.getShareMode());
        shareDetailResponse.setShareModeName(BizShareModeEnum.toName(share.getShareMode()));
        shareDetailResponse.setDescription(share.getDescription());
        shareDetailResponse.setEstimatePriceStart(share.getEstimatePriceStart());
        shareDetailResponse.setEstimatePriceEnd(share.getEstimatePriceEnd());
        shareDetailResponse.setCreatedTime(share.getCreateTime());
        shareDetailResponse.setStatus(share.getStatus());
        shareDetailResponse.setStatusText(BizSharedOrderStatus.text(share.getStatus()));
        shareDetailResponse.setSubStatus(share.getSubStatus());
        shareDetailResponse.setSubStatusText(BizSharedOrderStatus.SubStatus.text(share.getSubStatus()));
        shareDetailResponse.setTips(tips);
        shareDetailResponse.setRemark(share.getRemark());
        // 用户提交信息
        Detail userSubmit = new Detail();
        userSubmit.setProductType(ProductTypeEnums.valueOf(share.getProductType()).getDesc());
//        userSubmit.setBrand(share.getBrand());
        userSubmit.setCode(share.getCode());
        userSubmit.setQuality(QualityEnum.valueOf(share.getQuality()).getDesc());
        userSubmit.setParts(Arrays.stream(share.getParts().split(",")).map(p -> ProductParts.valueOf(p).getDesc()).collect(Collectors.joining()));
        shareDetailResponse.setUserSubmit(userSubmit);
        // 初评信息
        Detail initialEvaluation = new Detail();
        if (StringUtils.isNotEmpty(share.getProductTypeFinal())) {
            initialEvaluation.setProductType(ProductTypeEnums.valueOf(share.getProductTypeFinal()).getDesc());
        }
//        initialEvaluation.setBrand(share.getBrandFinal());
        initialEvaluation.setCode(share.getCodeFinal());
        if (StringUtils.isNotEmpty(share.getQualityFinal())) {
            initialEvaluation.setQuality(QualityEnum.valueOf(share.getQualityFinal()).getDesc());
        }
        if (StringUtils.isNotEmpty(share.getPartsFinal())) {
            initialEvaluation.setParts(Arrays.stream(share.getPartsFinal().split(",")).map(p -> ProductParts.valueOf(p).getDesc()).collect(Collectors.joining(",")));
        }
        initialEvaluation.setEstimatePriceStart(share.getEstimatePriceStart());
        initialEvaluation.setEstimatePriceEnd(share.getEstimatePriceEnd());
        shareDetailResponse.setInitialEvaluation(initialEvaluation);
        // 图片信息
        shareDetailResponse.setPics(bizImgs);
        return shareDetailResponse;
    }
}
